﻿import ajaxOptions = require("AjaxCallsOptions");
import config = require("Configuration");
import dataAccess = require("Data-access");
import application = require("WindowExtensions");
import models = require("Models");

export class TheaterMovieList extends kendo.data.ObservableObject {

    theaterList: any;
    selectedMovie: models.MovieForTheater;
    selectedDate: string;

    constructor() {
        super();
        super.init(this);
    }

    private _instance = this;

    getTheaters = (movieId: number) => {
        var theaterListOptions = new ajaxOptions.AjaxOptions();
        theaterListOptions.url = config.Configuration.getTheaterListForMovieUrl;
        theaterListOptions.requestType = "GET";
        theaterListOptions.dataType = "JSON";
        theaterListOptions.data = { movieId: movieId };

        dataAccess.callService(theaterListOptions, this._instance, this.fnGetTheatersCallBack);
    };

    fnGetTheatersCallBack(model: kendo.data.ObservableObject, result: ajaxOptions.AjaxOptionsCallbackInput) {
        if (result.success !== true) {
            alert("Error generated in fnGetTheatersCallBack");
        } else {
            model.set("theaterList", result.data);
        }
    }

    show(e) {
        this.set("selectedDate", new Date().toLocaleDateString());
        var movie = new models.MovieForTheater();
        movie.movieId = e.view.params.movieId;
        movie.movieName = decodeURIComponent(e.view.params.movieName);
        movie.rating = e.view.params.rating;
        movie.imageUrl = e.view.params.image;
        this.set("selectedMovie", movie);
        this.getTheaters(movie.movieId);
    }
}

var appInit = application.getAppInitProperty();
var viewData = application.getViewDataProperty();
appInit.theatersListInit = () => {
    var viewModel = new TheaterMovieList();
    viewData.TheatresViewModel = viewModel;
    kendo.bind($("#mt-theaters-movie-view"), viewModel);
}